/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "utils.h"
#include <QDebug>
#include <QRegularExpression>

namespace UKUI {

QString fromQListIntToString(const QList<int> sequence)
{
    QString itemSequence;
    if(!sequence.isEmpty()) {
        itemSequence = QString::number(sequence.first());
        for(int i = 1; i < sequence.size(); i++) {
            itemSequence += QString(",") += QString::number(sequence.at(i));
        }
    }
    return itemSequence;
}

QString fromQListUIntToString(const QList<quint32> sequence)
{
    QString itemSequence;
    if(!sequence.isEmpty()) {
        itemSequence = QString::number(sequence.first());
        for(int i = 1; i < sequence.size(); i++) {
            itemSequence += QString(",") += QString::number(sequence.at(i));
        }
    }
    return itemSequence;
}

QString fromQListUIntToString(const QList<int> sequence)
{
    QString itemSequence;
    if(!sequence.isEmpty()) {
        itemSequence = QString::number(sequence.first());
        for(int i = 1; i < sequence.size(); i++) {
            itemSequence += QString(",") += QString::number(sequence.at(i));
        }
    }
    return itemSequence;
}

QPair<int,int> fromStringToInt(const QString layoutString)
{
    QList<int> list;
    QPair<int,int> pair;
    QRegExp rx("\\d+");
    int index = 0;
    while ((index = rx.indexIn(layoutString, index)) != -1) {
        index += rx.matchedLength();
        list.push_back(rx.cap(0).toInt());
    }
    if (list.count() != 2) {
        qWarning() << "row or column is incorrect!!!" << __func__;
        return pair;
    }
    pair.first = list[0];
    pair.second = list[1];
    return pair;
}

QString translatedName(const QString &sourceName)
{
    static QMap<QString, QString> translatedNames = {
        { "All", QObject::tr("All") },
        { "Mobile", QObject::tr("Mobile") },
        { "Internet", QObject::tr("Internet") },
        { "Social", QObject::tr("Social") },
        { "Video", QObject::tr("Video") },
        { "Development", QObject::tr("Development") },
        { "Image", QObject::tr("Image") },
        { "Game", QObject::tr("Game") },
        { "Office", QObject::tr("Office") },
        { "Education", QObject::tr("Education") },
        { "System", QObject::tr("System") },
        { "Safe", QObject::tr("Safe") },
        { "Others", QObject::tr("Others") },
        { "Group", QObject::tr("Group") },
    };
    if (translatedNames.contains(sourceName)) {
        return translatedNames.value(sourceName);
    }
    static QRegularExpression regex("Group \\d+");
    auto match = regex.match(sourceName);
    if (match.hasMatch() && match.capturedLength() == sourceName.length()) {
        return translatedNames.value("Group") + " " +
                sourceName.split(" ").constLast();
    }

    return QString();
}

}
